package com.jt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.jt.entity.Order;
import com.jt.entity.Result;
import com.jt.entity.Storage;
import com.jt.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/storage")
public class StorageController {
    @Autowired
    private StorageService storageService;
    @PostMapping("/deleteStorage")
    public Result deleteStorage(@RequestBody Order order){
        Storage storage = storageService.getOne(new QueryWrapper<Storage>().eq("product_id", order.getProduct_id()));
        storage.setTotal(storage.getTotal()-order.getCount());
        boolean update = storageService.update(storage, new QueryWrapper<Storage>().eq("product_id", order.getProduct_id()));
        if ( update){
            return Result.success("库存扣减成功");
        }
        return Result.error("库存扣减失败");
    }
}
